package com.tonann.web.api;


import com.tonann.app.VO.FaultVO;
import com.tonann.app.domain.AppDevice;
import com.tonann.app.domain.AppDevicePlaces;
import com.tonann.app.domain.AppSensor;
import com.tonann.app.domain.Place;
import com.tonann.app.mapper.AppDevicePlacesMapper;
import com.tonann.app.mapper.PlaceMapper;
import com.tonann.app.service.IAppDeviceService;
import com.tonann.app.service.IAppFaultService;
import com.tonann.app.service.IAppSensorService;
import com.tonann.common.constant.CacheConstants;
import com.tonann.common.core.domain.AjaxResult;
import com.tonann.common.utils.bean.BeanUtils;
import com.tonann.web.api.service.WeiYunApi;
import com.tonann.web.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@Api("三维端接口管理")
@RestController
@RequestMapping("/dashboard/api")
public class DashboardApi {

    @Resource
    private IAppDeviceService deviceService;

    @Resource
    private IAppSensorService sensorService;

    @Resource
    private IAppFaultService faultService;

    @Resource
    private PlaceMapper placeService;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private AppDevicePlacesMapper appDevicePlacesMapper;

    @Resource
    private WeiYunApi weiYunApi;

    @ApiOperation(value = "首页通排风系统")
    @GetMapping("/index/exhaust")
    public AjaxResult exhaust() {
        return AjaxResult.success(deviceService.getIndexExhaust(2L));
    }

    @ApiOperation(value = "风机系统")
    @GetMapping("/index/fun")
    public AjaxResult fun() throws Exception {
        AppDevice appDevice = new AppDevice();
        appDevice.setType("3");
        appDevice.setSubCount(0L);
        appDevice.setProjectId(2L);
        List<AppDevice> deviceList = deviceService.selectAppDeviceAll(appDevice);
        List<Long> deviceIds = new ArrayList<>();
        Map<Long, AppDevice> deviceMap = new HashMap<>();
        for (AppDevice device : deviceList) {
            deviceIds.add(device.getDeviceId());
            deviceMap.put(device.getDeviceId(), device);
        }
        List<AppSensor> sensorList = sensorService.selectAppSensorAllListByDeviceIds(deviceIds);
        Map<Long, FunVO> funVOMap = new HashMap<>();
        for (AppSensor sensor : sensorList) {
            switch (sensor.getSensorType()) {
                case "4":
                    if (funVOMap.get(sensor.getDeviceId()) == null) {
                        FunVO vo = new FunVO();
                        BeanUtils.copyBeanProp(vo, deviceMap.get(sensor.getDeviceId()));
                        String s = null;
                        Object object = redisTemplate.opsForValue().get(CacheConstants.APP_SENSOR_VALUE_KEY + sensor.getDeviceId() + ":sensorId:" + sensor.getSensorId());
                        if (object != null) {
                            if (object instanceof String) {
                                s = object.toString();
                            }
                            if (object instanceof BigDecimal) {
                                s = ((BigDecimal) object).toString();
                            }
                        }
                        if (s != null) {
                            vo.setAirVolume(s);
                        }
                        funVOMap.put(sensor.getDeviceId(), vo);
                    } else {
                        FunVO vo = funVOMap.get(sensor.getDeviceId());
                        String s = null;
                        Object object = redisTemplate.opsForValue().get(CacheConstants.APP_SENSOR_VALUE_KEY + sensor.getDeviceId() + ":sensorId:" + sensor.getSensorId());
                        if (object != null) {
                            if (object instanceof String) {
                                s = object.toString();
                            }
                            if (object instanceof BigDecimal) {
                                s = ((BigDecimal) object).toString();
                            }
                        }
                        if (s != null) {
                            vo.setAirVolume(s);
                        }
                        funVOMap.put(sensor.getDeviceId(), vo);
                    }
                    break;
                case "10":
                    if (funVOMap.get(sensor.getDeviceId()) == null) {
                        FunVO vo = new FunVO();
                        BeanUtils.copyBeanProp(vo, deviceMap.get(sensor.getDeviceId()));
                        String s = null;
                        Object object = redisTemplate.opsForValue().get(CacheConstants.APP_SENSOR_VALUE_KEY + sensor.getDeviceId() + ":sensorId:" + sensor.getSensorId());
                        if (object != null) {
                            if (object instanceof String) {
                                s = object.toString();
                            }
                            if (object instanceof BigDecimal) {
                                s = ((BigDecimal) object).toString();
                            }
                        }
                        if (s != null) {
                            vo.setFrequency(s);
                        }
                        funVOMap.put(sensor.getDeviceId(), vo);
                    } else {
                        FunVO vo = funVOMap.get(sensor.getDeviceId());
                        String s = null;
                        Object object = redisTemplate.opsForValue().get(CacheConstants.APP_SENSOR_VALUE_KEY + sensor.getDeviceId() + ":sensorId:" + sensor.getSensorId());
                        if (object != null) {
                            if (object instanceof String) {
                                s = object.toString();
                            }
                            if (object instanceof BigDecimal) {
                                s = ((BigDecimal) object).toString();
                            }
                        }
                        if (s != null) {
                            vo.setFrequency(s);
                        }
                        funVOMap.put(sensor.getDeviceId(), vo);
                    }
                    break;
                case "11":
                    if (funVOMap.get(sensor.getDeviceId()) == null) {
                        FunVO vo = new FunVO();
                        BeanUtils.copyBeanProp(vo, deviceMap.get(sensor.getDeviceId()));
                        String s = null;
                        Object object = redisTemplate.opsForValue().get(CacheConstants.APP_SENSOR_VALUE_KEY + sensor.getDeviceId() + ":sensorId:" + sensor.getSensorId());
                        if (object != null) {
                            if (object instanceof String) {
                                s = object.toString();
                            }
                            if (object instanceof BigDecimal) {
                                s = ((BigDecimal) object).toString();
                            }
                        }
                        if (s != null) {
                            vo.setCurrent(s);
                        }
                        funVOMap.put(sensor.getDeviceId(), vo);
                    } else {
                        FunVO vo = funVOMap.get(sensor.getDeviceId());
                        String s = null;
                        Object object = redisTemplate.opsForValue().get(CacheConstants.APP_SENSOR_VALUE_KEY + sensor.getDeviceId() + ":sensorId:" + sensor.getSensorId());
                        if (object != null) {
                            if (object instanceof String) {
                                s = object.toString();
                            }
                            if (object instanceof BigDecimal) {
                                s = ((BigDecimal) object).toString();
                            }
                        }
                        if (s != null) {
                            vo.setCurrent(s);
                        }
                        funVOMap.put(sensor.getDeviceId(), vo);
                    }
                    break;
                case "12":
                    if (funVOMap.get(sensor.getDeviceId()) == null) {
                        FunVO vo = new FunVO();
                        BeanUtils.copyBeanProp(vo, deviceMap.get(sensor.getDeviceId()));
                        String s = null;
                        Object object = redisTemplate.opsForValue().get(CacheConstants.APP_SENSOR_VALUE_KEY + sensor.getDeviceId() + ":sensorId:" + sensor.getSensorId());
                        if (object != null) {
                            if (object instanceof String) {
                                s = object.toString();
                            }
                            if (object instanceof BigDecimal) {
                                s = ((BigDecimal) object).toString();
                            }
                        }
                        if (s != null) {
                            vo.setVoltage(s);
                        }
                        funVOMap.put(sensor.getDeviceId(), vo);
                    } else {
                        FunVO vo = funVOMap.get(sensor.getDeviceId());
                        String s = null;
                        Object object = redisTemplate.opsForValue().get(CacheConstants.APP_SENSOR_VALUE_KEY + sensor.getDeviceId() + ":sensorId:" + sensor.getSensorId());
                        if (object != null) {
                            if (object instanceof String) {
                                s = object.toString();
                            }
                            if (object instanceof BigDecimal) {
                                s = ((BigDecimal) object).toString();
                            }
                        }
                        if (s != null) {
                            vo.setVoltage(s);
                        }
                        funVOMap.put(sensor.getDeviceId(), vo);
                    }
                    break;
                case "22":
                    if (funVOMap.get(sensor.getDeviceId()) == null) {
                        FunVO vo = new FunVO();
                        BeanUtils.copyBeanProp(vo, deviceMap.get(sensor.getDeviceId()));
                        String s = null;
                        Object object = redisTemplate.opsForValue().get(CacheConstants.APP_SENSOR_VALUE_KEY + sensor.getDeviceId() + ":sensorId:" + sensor.getSensorId());
                        if (object != null) {
                            if (object instanceof String) {
                                s = object.toString();
                            }
                            if (object instanceof BigDecimal) {
                                s = ((BigDecimal) object).toString();
                            }
                        }
                        if (s != null) {
                            vo.setVoltage(s);
                        }
                        funVOMap.put(sensor.getDeviceId(), vo);
                    } else {
                        FunVO vo = funVOMap.get(sensor.getDeviceId());
                        String s = null;
                        Object object = redisTemplate.opsForValue().get(CacheConstants.APP_SENSOR_VALUE_KEY + sensor.getDeviceId() + ":sensorId:" + sensor.getSensorId());
                        if (object != null) {
                            if (object instanceof String) {
                                s = object.toString();
                            }
                            if (object instanceof BigDecimal) {
                                s = ((BigDecimal) object).toString();
                            }
                        }
                        if (s != null && sensor.getIsOn().equals("1")) {
                            int value = 0;
                            BigDecimal bigDecimal = new BigDecimal(s);
                            value = bigDecimal.intValue();
                            vo.setIsRunning(Integer.toString(value));
                        }
                        funVOMap.put(sensor.getDeviceId(), vo);
                    }
                    break;
            }
        }
        List<FunVO> voList = new ArrayList<>(funVOMap.values());
        return AjaxResult.success(voList);
    }

    @ApiOperation(value = "风机系统详情")
    @GetMapping("/index/fun/{deviceCode}")
    public AjaxResult funDetail(@PathVariable String deviceCode) throws Exception {
        AppDevice appDevice = new AppDevice();
        appDevice.setType("3");
        appDevice.setSubCount(0L);
        appDevice.setProjectId(2L);
        appDevice.setDeviceCode(deviceCode);
        List<AppDevice> deviceList = deviceService.selectAppDeviceAll(appDevice);
        FunVO vo = new FunVO();
        if (!deviceList.isEmpty()) {
            AppDevice device = deviceList.get(0);
            AppSensor appSensor = new AppSensor();
            appSensor.setDeviceId(device.getDeviceId());
            List<AppSensor> sensorList = sensorService.selectAppSensorAllList(appSensor);
            BeanUtils.copyBeanProp(vo, device);
            Set<Long> set = new HashSet<>();
            if (!sensorList.isEmpty()) {
                for (AppSensor sensor : sensorList) {
                    String s = null;
                    Object object = redisTemplate.opsForValue().get(CacheConstants.APP_SENSOR_VALUE_KEY + sensor.getDeviceId() + ":sensorId:" + sensor.getSensorId());
                    if (object != null) {
                        if (object instanceof String) {
                            s = object.toString();
                        }
                        if (object instanceof BigDecimal) {
                            s = ((BigDecimal) object).toString();
                        }
                    }
                    switch (sensor.getSensorType()) {

                        case "4":
                            if (s != null) {
                                vo.setAirVolume(s);
                            }
                            break;
                        case "10":
                            if (s != null) {
                                vo.setFrequency(s);
                            }
                            break;
                        case "11":
                            if (s != null) {
                                vo.setCurrent(s);
                            }
                            break;
                        case "12":
                            if (s != null) {
                                vo.setVoltage(s);
                            }
                            break;
                    }
                }
            }
            if (vo.getAirVolume() == null) {
                vo.setAirVolume("0");
            }
            if (!set.isEmpty()) {
                List<FaultVO> faultVOList = faultService.getDeviceFaultName(set);
                vo.setFaultList(faultVOList);
            }
        }
        return AjaxResult.success(vo);
    }

    @ApiOperation(value = "通风柜系统")
    @GetMapping("/index/fumeHood")
    public AjaxResult fumeHood() throws Exception {
        AppDevice appDevice = new AppDevice();
        appDevice.setType("2");
        appDevice.setSubCount(0L);
        appDevice.setProjectId(2L);
        List<AppDevice> deviceList = deviceService.selectAppDeviceAll(appDevice);
        FumeHoodVO fumeHoodVO = new FumeHoodVO();
        fumeHoodVO.setTotal(deviceList.size());
        int running = 0;
        int exception = 0;
        Set<String> floorSet = new HashSet<>();
        Map<String, List<AppDevice>> appDeviceListHashMap = new HashMap<>();
        Map<String, Set<Long>> deviceIdMap = new HashMap<>();
        List<Long> deviceIds = new ArrayList<>();
        Map<Long, AppDevice> deviceMap = new HashMap<>();
        for (AppDevice device : deviceList) {
            if (device.getIsException() != null && device.getIsException().equals("1")) {
                exception++;
            }
            deviceIds.add(device.getDeviceId());
            deviceMap.put(device.getDeviceId(), device);
        }
        if (deviceIds.isEmpty()) {
            return AjaxResult.success();
        }
        List<AppDevicePlaces> placesList = appDevicePlacesMapper.selectAppDevicePlacesListByDeviceIds(deviceIds);
        for (AppDevicePlaces places : placesList) {
            if (places.getPlace() != null) {
                Place sysPlace = places.getPlace();
                Place place = null;
                if (sysPlace.getParentId() != null && sysPlace.getParentId() != 0L) {
                    String ancestors = sysPlace.getAncestors();
                    if (ancestors != null) {
                        String[] strs = ancestors.split(",");
                        if (strs.length > 2) {
                            String str = strs[2];
                            Long placeId = Long.valueOf(str);
                            place = placeService.selectPlaceById(placeId);
                        }
                    }
                }
                if (place != null) {
                    if (floorSet.contains(place.getPlaceName())) {
                        Set<Long> set = deviceIdMap.get(place.getPlaceName());
                        if (!set.contains(places.getDeviceId())) {
                            List<AppDevice> appDeviceList = appDeviceListHashMap.get(place.getPlaceName());
                            appDeviceList.add(deviceMap.get(places.getDeviceId()));
                            appDeviceListHashMap.put(place.getPlaceName(), appDeviceList);
                            set.add(places.getPlaceId());
                            deviceIdMap.put(place.getPlaceName(),set);
                        }
                    } else {
                        floorSet.add(place.getPlaceName());
                        List<AppDevice> appDeviceList = new ArrayList<>();
                        appDeviceList.add(deviceMap.get(places.getDeviceId()));
                        appDeviceListHashMap.put(place.getPlaceName(), appDeviceList);
                        Set<Long> set = new HashSet<>();
                        set.add(places.getDeviceId());
                        deviceIdMap.put(place.getPlaceName(),set);
                    }
                }
            }
        }
        List<AppSensor> funSensorList = sensorService.selectAppSensorAllListByDeviceIds(deviceIds);
        Map<Long, Integer> deviceRunningMap = new HashMap<>();
        for (AppSensor sensor : funSensorList) {
            if (sensor.getSensorType() != null && sensor.getSensorType().equals("24")) {
                if (sensor.getSensorName() != null && sensor.getSensorName().toUpperCase().contains("REM") && !sensor.getSensorName().toLowerCase().contains("set")) {
                    Object object = redisTemplate.opsForValue().get(CacheConstants.APP_SENSOR_VALUE_KEY + sensor.getDeviceId() + ":sensorId:" + sensor.getSensorId());
                    if (object != null && !object.toString().isEmpty()) {
                        BigDecimal bigDecimal = new BigDecimal(object.toString());
                        if (bigDecimal.intValue() == 1) {
                            running++;
                            deviceRunningMap.merge(sensor.getDeviceId(), 1, Integer::sum);
                        }
                    }

                }
            }
        }
        List<FumeHoodFloorVO> fumeHoodFloorVOList = new ArrayList<>();
        for (String placeName : appDeviceListHashMap.keySet()) {
            FumeHoodFloorVO fumeHoodFloorVO = new FumeHoodFloorVO();
            List<AppDevice> devices = appDeviceListHashMap.get(placeName);
            int floorRunning = 0;
            int floorException = 0;
            for (AppDevice d : devices) {
                if (d.getIsException() != null && d.getIsException().equals("1")) {
                    floorException++;
                }
                if (deviceRunningMap.get(d.getDeviceId()) != null) {
                    floorRunning += deviceRunningMap.get(d.getDeviceId());
                }
            }
            fumeHoodFloorVO.setRunning(floorRunning);
            fumeHoodFloorVO.setException(floorException);
            fumeHoodFloorVO.setClose(devices.size() - floorRunning);
            fumeHoodFloorVO.setFloorName(placeName);
            fumeHoodFloorVOList.add(fumeHoodFloorVO);
        }
        fumeHoodVO.setDeviceList(deviceList);
        fumeHoodVO.setFumeHoodFloorList(fumeHoodFloorVOList);
        fumeHoodVO.setClose(deviceList.size() - running);
        fumeHoodVO.setRunning(running);
        fumeHoodVO.setException(exception);

        return AjaxResult.success(fumeHoodVO);
    }

    @ApiOperation(value = "通风柜系统详情")
    @GetMapping("/index/fumeHood/{deviceCode}")
    public AjaxResult fumeHoodDetail(@PathVariable String deviceCode) throws Exception {
        AppDevice appDevice = new AppDevice();
        appDevice.setType("2");
        appDevice.setDeviceCode(deviceCode);
        List<AppDevice> deviceList = deviceService.selectAppDeviceAll(appDevice);
        FumeHoodDetailVO vo = new FumeHoodDetailVO();
        if (!deviceList.isEmpty()) {
            AppDevice device = deviceList.get(0);
            AppSensor appSensor = new AppSensor();
            appSensor.setDeviceId(device.getDeviceId());
            List<AppSensor> sensorList = sensorService.selectAppSensorAllList(appSensor);
            BeanUtils.copyBeanProp(vo, device);
            Set<Long> set = new HashSet<>();
            if (!sensorList.isEmpty()) {
                for (AppSensor sensor : sensorList) {
                    String s = null;
                    Object object = redisTemplate.opsForValue().get(CacheConstants.APP_SENSOR_VALUE_KEY + sensor.getDeviceId() + ":sensorId:" + sensor.getSensorId());
                    if (object != null) {
                        if (object instanceof String) {
                            s = object.toString();
                        }
                        if (object instanceof BigDecimal) {
                            s = ((BigDecimal) object).toString();
                        }
                    }
                    if (s != null) {
                        set.add(sensor.getSensorId());
                        if (sensor.getSensorType().equals("4")) {  //风量
                            vo.setAirVolume(s);
                        } else if (sensor.getSensorType().equals("9")) { //风速
                            vo.setAirSpeed(s);
                        } else if (sensor.getSensorType().equals("8")) {   //窗高度
                            vo.setWindowHeight(s);
                        } else if (sensor.getSensorType().equals("13")) {  //阀门开度
                            vo.setAngle(s);
                        } else if (sensor.getSensorType().equals("1")) {  //温度
                            vo.setTemperature(s);
                        } else if (sensor.getUnit().equals("开关量")) {
                            if (!s.isEmpty()) {
                                BigDecimal bigDecimal = new BigDecimal(s);
                                s = (bigDecimal.intValue() + "");
                            }
                            if (sensor.getIsLight().equals("1")) {
                                if (s.equals("0")) {
                                    vo.setLight("关闭");
                                } else {
                                    vo.setLight("开启");
                                }
                            } else if (sensor.getIsOn().equals("1")) {
                                vo.setIsRunning(s);
                            }
                        }
                    }

                }
            }
            if (!set.isEmpty()) {
                List<FaultVO> faultVOList = faultService.getDeviceFaultName(set);
                vo.setFaultList(faultVOList);
            }
            if (vo.getAirVolume() == null) {
                vo.setAirVolume("0");
            }
            if (vo.getAirSpeed() == null) {
                vo.setAirSpeed("0");
            }
            if (vo.getAngle() == null) {
                vo.setAngle("0");
            }
            if (vo.getWindowHeight() == null) {
                vo.setWindowHeight("0");
            }
            if (vo.getTemperature() == null) {
                vo.setTemperature("0");
            }
            if (vo.getLight() == null) {
                vo.setLight("0");
            }
        }
        return AjaxResult.success(vo);
    }

    @ApiOperation(value = "气站系统")
    @GetMapping("/index/gas")
    public AjaxResult gasStation() {
        List<GasStationVO> voList = new ArrayList<>();
        AppDevice gasStation = new AppDevice();
        gasStation.setType("8");
        gasStation.setDelFlag("0");
        gasStation.setStatus("0");
        gasStation.setProjectId(2L);
        //获取气站设备的顶级设备
        List<AppDevice> gasAppDeviceList = deviceService.selectAppDeviceList(gasStation);
        //电磁阀设备列表
        List<AppDevice> valveList = gasAppDeviceList.stream().filter(item -> item.getDeviceName().contains("电磁阀")).collect(Collectors.toList());
        //流量计
        List<AppDevice> flowmeterList = gasAppDeviceList.stream().filter(item -> item.getDeviceName().contains("流量计")).collect(Collectors.toList());
        //气瓶
        List<AppDevice> bottleList = gasAppDeviceList.stream().filter(item ->
                (item.getDeviceName().contains("左瓶") || item.getDeviceName().contains("右瓶")) &&
                        !item.getDeviceName().contains("气瓶柜")
        ).collect(Collectors.toList());
        //气瓶柜
        List<AppDevice> cabinetList = gasAppDeviceList.stream().filter(item ->
                item.getDeviceName().contains("气瓶柜") &&
                        !(item.getDeviceName().contains("左瓶") || item.getDeviceName().contains("右瓶"))).collect(Collectors.toList());
        //钢瓶电子秤
        List<AppDevice> electronicScaleList = gasAppDeviceList.stream().filter(item -> item.getDeviceName().contains("钢瓶电子称")).collect(Collectors.toList());
        //泄露侦测
        List<AppDevice> leakList = gasAppDeviceList.stream().filter(item -> item.getDeviceName().contains("探头")).collect(Collectors.toList());

        List<AppDevice> appDeviceList1 = gasAppDeviceList.stream().filter(item -> item.getParentId() == 0L).collect(Collectors.toList()); //总设备层级
        List<AppDevice> appDeviceList2 = new ArrayList<>(); //气站层级
        List<AppDevice> appDeviceList3 = new ArrayList<>(); //气瓶层级
        List<Long> topGasDeviceIds = appDeviceList1.stream().map(AppDevice::getDeviceId).collect(Collectors.toList());
        appDeviceList2 = gasAppDeviceList.stream().filter(item -> topGasDeviceIds.contains(item.getParentId()) && item.getDeviceName().contains("气站")).collect(Collectors.toList());
        List<Long> secondGasDeviceIds = appDeviceList2.stream().map(AppDevice::getDeviceId).collect(Collectors.toList());
        appDeviceList3 = gasAppDeviceList.stream().filter(item -> secondGasDeviceIds.contains(item.getParentId()) && item.getDeviceName().contains("瓶")).collect(Collectors.toList());
        List<AppDevice> lastGasDeviceList = gasAppDeviceList.stream().filter(item -> item.getSubCount() == 0L).collect(Collectors.toList());
        List<Long> lastDeviceIds = lastGasDeviceList.stream().map(AppDevice::getDeviceId).collect(Collectors.toList());
        List<AppSensor> lastSensor = sensorService.selectAppSensorAllListByDeviceIds(lastDeviceIds);
        List<AppSensor> volumeSensorList = lastSensor.stream().filter(item -> item.getSensorType().equals("31") && item.getSensorName().contains("瓶")).collect(Collectors.toList());
        List<AppSensor> runningSensorList = lastSensor.stream().filter(item -> item.getSensorType().equals("22")).collect(Collectors.toList());
        Map<Long, List<AppSensor>> volumeSensorMap = volumeSensorList.stream().collect(Collectors.groupingBy(AppSensor::getDeviceId));
        Map<Long, List<AppSensor>> runningSensorMap = runningSensorList.stream().collect(Collectors.groupingBy(AppSensor::getDeviceId));
        Map<Long, List<AppDevice>> thirdGasDeviceMap = appDeviceList3.stream().collect(Collectors.groupingBy(AppDevice::getParentId));
        for (AppDevice gasDevice : appDeviceList2) {
            GasStationVO vo = new GasStationVO();
            vo.setDevice(gasDevice);
            Integer airBottle = 0;
            Integer exceptionAirBottle = 0;
            //泄露侦测数
            Integer totalGasLeak = 0;
            //告警的泄露侦测
            Integer exceptionGasLeak = 0;
            vo.setTotalGasLeak(totalGasLeak);
            vo.setExceptionGasLeak(exceptionGasLeak);
            List<AppDevice> list = thirdGasDeviceMap.get(gasDevice.getDeviceId());
            if (list != null) {
                for (AppDevice device : list) {
                    airBottle++;
                    if (device.getIsRunning() != null && device.getIsException().equals("1")) {
                        exceptionAirBottle++;
                    }
                }
            }
            vo.setAirBottle(airBottle);
            vo.setExceptionAirBottle(exceptionAirBottle);
            //获取气路流量
            //氦气用量
            int gasHe = 0;
            //氧气
            int gasO2 = 0;
            //氢气
            int gasH2 = 0;
            //氩气 Ar
            int gasAr = 0;
            //氮气
            int gasN2 = 0;
            //乙炔
            int gasC2H2 = 0;
            //二氧化碳
            int gasCO2 = 0;
            for (AppDevice lastDevice : lastGasDeviceList) {
                if (lastDevice.getAncestors().contains(gasDevice.getDeviceId().toString()) && volumeSensorMap.get(lastDevice.getDeviceId()) != null) {
                    for (AppSensor sensor : volumeSensorMap.get(lastDevice.getDeviceId())) {
                        if (sensor.getSensorName().contains("累计流量")) {
                            BigDecimal bigDecimal = null;
                            String s = null;
                            Object object = redisTemplate.opsForValue().get(CacheConstants.APP_SENSOR_VALUE_KEY + sensor.getDeviceId() + ":sensorId:" + sensor.getSensorId());
                            if (object != null) {
                                if (object instanceof String) {
                                    s = object.toString();
                                }
                                if (object instanceof BigDecimal) {
                                    s = ((BigDecimal) object).toString();
                                }
                            }
                            if (s != null) {
                                String value = s;
                                value = this.replaceSpecStr(value);
                                if (value != null) {
                                    bigDecimal = new BigDecimal(s);
                                } else {
                                    bigDecimal = new BigDecimal("0");
                                }
                            } else {
                                bigDecimal = new BigDecimal("0");
                            }
                            if (sensor.getSensorName().toLowerCase().contains("he")) {
                                gasHe += bigDecimal.intValue();
                            } else if (sensor.getSensorName().toLowerCase().contains("o2") && !sensor.getSensorName().toLowerCase().contains("co2")) {
                                gasO2 += bigDecimal.intValue();
                            } else if (sensor.getSensorName().toLowerCase().contains("h2")) {
                                gasH2 += bigDecimal.intValue();
                            } else if (sensor.getSensorName().toLowerCase().contains("ar")) {
                                gasAr += bigDecimal.intValue();
                            } else if (sensor.getSensorName().toLowerCase().contains("n2")) {
                                gasN2 += bigDecimal.intValue();
                            } else if (sensor.getSensorName().toLowerCase().contains("c2h2")) {
                                gasC2H2 += bigDecimal.intValue();
                            } else if (sensor.getSensorName().toLowerCase().contains("co2")) {
                                gasCO2 += bigDecimal.intValue();
                            }
                        }
                    }
                    if (runningSensorMap.get(lastDevice.getDeviceId()) != null) {
                        for (AppSensor sensor : runningSensorMap.get(lastDevice.getDeviceId())) {
                            String s = null;
                            Object object = redisTemplate.opsForValue().get(CacheConstants.APP_SENSOR_VALUE_KEY + sensor.getDeviceId() + ":sensorId:" + sensor.getSensorId());
                            if (object != null) {
                                if (object instanceof String) {
                                    s = object.toString();
                                }
                                if (object instanceof BigDecimal) {
                                    s = ((BigDecimal) object).toString();
                                }
                            }
                            if (s != null && sensor.getIsOn().equals("1")) {
                                int value = 0;
                                BigDecimal bigDecimal = new BigDecimal(s);
                                value = bigDecimal.intValue();
                                vo.getDevice().setIsRunning(Integer.toString(value));
                            }
                        }
                    }
                }
            }
            vo.setGasHe(gasHe);
            //氧气
            vo.setGasO2(gasO2);

            //氢气
            vo.setGasH2(gasH2);

            //氩气 Ar
            vo.setGasAr(gasAr);

            //氮气
            vo.setGasN2(gasN2);

            //乙炔
            vo.setGasC2H2(gasC2H2);

            //二氧化碳
            vo.setGasCO2(gasCO2);

            //电磁阀设备列表
            vo.setValveList(valveList);
            //流量计
            vo.setFlowmeterList(flowmeterList);
            //气瓶
            vo.setBottleList(bottleList);
            //气瓶柜
            vo.setCabinetList(cabinetList);
            //钢瓶电子秤
            vo.setElectronicScaleList(electronicScaleList);
            //泄露侦测
            vo.setLeakList(leakList);

            voList.add(vo);
        }
        return AjaxResult.success(voList);
    }

    @ApiOperation(value = "电磁阀状态")
    @GetMapping("/index/gas/valve/{deviceCode}")
    public AjaxResult valve(@PathVariable String deviceCode) {
        AppDevice appDevice = new AppDevice();
        appDevice.setProjectId(2L);
        appDevice.setDeviceCode(deviceCode);
        List<AppDevice> deviceList = deviceService.selectAppDeviceAll(appDevice);
        if (!deviceList.isEmpty()) {
            appDevice = deviceList.get(0);
            AppSensor appSensor = new AppSensor();
            appSensor.setDeviceId(appDevice.getDeviceId());
            appSensor.setSensorType("22");
            List<AppSensor> sensorList = sensorService.selectAppSensorAllList(appSensor);
            if (!sensorList.isEmpty()) {
                for (AppSensor sensor : sensorList) {
                    String s = null;
                    Object object = redisTemplate.opsForValue().get(CacheConstants.APP_SENSOR_VALUE_KEY + sensor.getDeviceId() + ":sensorId:" + sensor.getSensorId());
                    if (object != null) {
                        if (object instanceof String) {
                            s = object.toString();
                        }
                        if (object instanceof BigDecimal) {
                            s = ((BigDecimal) object).toString();
                        }
                    }
                    if (s != null && sensor.getIsOn().equals("1")) {
                        int value = 0;
                        BigDecimal bigDecimal = new BigDecimal(s);
                        value = bigDecimal.intValue();
                        appDevice.setIsRunning(Integer.toString(value));
                    }
                }
            }
            return AjaxResult.success(appDevice);
        }
        return AjaxResult.error();
    }

    @ApiOperation(value = "流量计")
    @GetMapping("/index/gas/flowmeter/{deviceCode}")
    public AjaxResult flowmeter(@PathVariable String deviceCode) {
        AppDevice appDevice = new AppDevice();
        appDevice.setProjectId(2L);
        appDevice.setDeviceCode(deviceCode);
        Map<String, String> map = new HashMap<>();
        map.put("name", "");
        map.put("isOn", "1");
        map.put("currentFlow", "");
        map.put("sumFlow", "");
        List<AppDevice> deviceList = deviceService.selectAppDeviceAll(appDevice);
        if (!deviceList.isEmpty()) {
            appDevice = deviceList.get(0);
            AppSensor appSensor = new AppSensor();
            appSensor.setDeviceId(appDevice.getDeviceId());
            List<AppSensor> sensorList = sensorService.selectAppSensorAllList(appSensor);
            List<AppSensor> statusSensorList = sensorList.stream().filter(item -> item.getSensorType().equals("22")).collect(Collectors.toList());
            List<AppSensor> valueSensorList = sensorList.stream().filter(item -> item.getSensorType().equals("31")).collect(Collectors.toList());
            if (!statusSensorList.isEmpty()) {
                for (AppSensor sensor : statusSensorList) {
                    String s = null;
                    Object object = redisTemplate.opsForValue().get(CacheConstants.APP_SENSOR_VALUE_KEY + sensor.getDeviceId() + ":sensorId:" + sensor.getSensorId());
                    if (object != null) {
                        if (object instanceof String) {
                            s = object.toString();
                        }
                        if (object instanceof BigDecimal) {
                            s = ((BigDecimal) object).toString();
                        }
                    }
                    if (s != null && sensor.getIsOn().equals("1")) {
                        int value = 0;
                        BigDecimal bigDecimal = new BigDecimal(s);
                        value = bigDecimal.intValue();
                        appDevice.setIsRunning(Integer.toString(value));
                    }
                }
            }
            map.put("name", appDevice.getDeviceName());
            map.put("isOn", appDevice.getIsRunning());
            for (AppSensor sensor : valueSensorList) {
                String name = sensor.getSensorName();
                if (name != null) {
                    String s = null;
                    Object object = redisTemplate.opsForValue().get(CacheConstants.APP_SENSOR_VALUE_KEY + sensor.getDeviceId() + ":sensorId:" + sensor.getSensorId());
                    if (object != null) {
                        if (object instanceof String) {
                            s = object.toString();
                        }
                        if (object instanceof BigDecimal) {
                            s = ((BigDecimal) object).toString();
                        }
                    }
                    String flow = "";
                    if (s != null) {
                        flow = s;
                    } else {
                        flow = "0";
                    }
                    if (name.contains("瞬时")) {
                        map.put("currentFlow", flow);
                    } else if (name.contains("累计")) {
                        map.put("sumFlow", flow);
                    }
                }
            }
            return AjaxResult.success(map);
        }
        return AjaxResult.error();
    }

    @ApiOperation(value = "气瓶")
    @GetMapping("/index/gas/bottle/{deviceCode}")
    public AjaxResult bottle(@PathVariable String deviceCode) {
        AppDevice appDevice = new AppDevice();
        appDevice.setProjectId(2L);
        appDevice.setDeviceCode(deviceCode);
        Map<String, String> map = new HashMap<>();
        map.put("name", "");
        map.put("pressure", "");
        map.put("alarm", "");
        List<AppDevice> deviceList = deviceService.selectAppDeviceAll(appDevice);
        if (!deviceList.isEmpty()) {
            appDevice = deviceList.get(0);
            map.put("name", appDevice.getDeviceName());
            AppSensor appSensor = new AppSensor();
            appSensor.setDeviceId(appDevice.getDeviceId());
            List<AppSensor> sensorList = sensorService.selectAppSensorAllList(appSensor);
            for (AppSensor sensor : sensorList) {
                String name = sensor.getSensorName();
                if (name != null) {
                    if (name.contains("压力") && sensor.getSensorType().equals("3")) {
                        String s = null;
                        Object object = redisTemplate.opsForValue().get(CacheConstants.APP_SENSOR_VALUE_KEY + sensor.getDeviceId() + ":sensorId:" + sensor.getSensorId());
                        if (object != null) {
                            if (object instanceof String) {
                                s = object.toString();
                            }
                            if (object instanceof BigDecimal) {
                                s = ((BigDecimal) object).toString();
                            }
                        }
                        if (s != null) {
                            map.put("pressure", s);
                        }
                    }
                    if (name.contains("压力报警") && !name.contains("屏蔽")) {
                        String s = null;
                        Object object = redisTemplate.opsForValue().get(CacheConstants.APP_SENSOR_VALUE_KEY + sensor.getDeviceId() + ":sensorId:" + sensor.getSensorId());
                        if (object != null) {
                            if (object instanceof String) {
                                s = object.toString();
                            }
                            if (object instanceof BigDecimal) {
                                s = ((BigDecimal) object).toString();
                            }
                        }
                        if (s != null) {
                            map.put("alarm", s);
                        }
                    }
                }
            }
            return AjaxResult.success(map);
        }
        return AjaxResult.error();
    }


    @ApiOperation(value = "特殊气瓶柜")
    @GetMapping("/index/gas/cabinet/{deviceCode}")
    public AjaxResult cabinet(@PathVariable String deviceCode) {
        AppDevice appDevice = new AppDevice();
        appDevice.setProjectId(2L);
        appDevice.setDeviceCode(deviceCode);
        Map<String, Object> voMap = new HashMap<>();
        List<AppDevice> deviceList = deviceService.selectAppDeviceAll(appDevice);
        if (!deviceList.isEmpty()) {
            appDevice = deviceList.get(0);
            voMap.put("name", appDevice.getDeviceName());
            AppDevice appDeviceChild = new AppDevice();
            appDeviceChild.setProjectId(2L);
            appDeviceChild.setParentId(appDevice.getDeviceId());
            List<AppDevice> deviceChildList = deviceService.selectAppDeviceAll(appDeviceChild);
            List<Map<String, String>> mapList = new ArrayList<>();
            for (AppDevice deviceChild : deviceChildList) {
                Map<String, String> map = new HashMap<>();
                map.put("pressure", "");
                map.put("lowAlarm", "");
                map.put("overAlarm", "");
                map.put("name", deviceChild.getDeviceName());
                AppSensor appSensor = new AppSensor();
                appSensor.setDeviceId(deviceChild.getDeviceId());
                List<AppSensor> sensorList = sensorService.selectAppSensorAllList(appSensor);
                for (AppSensor sensor : sensorList) {
                    String name = sensor.getSensorName();
                    if (name != null) {
                        if (name.contains("压力") && sensor.getSensorType().equals("3")) {
                            String s = null;
                            Object object = redisTemplate.opsForValue().get(CacheConstants.APP_SENSOR_VALUE_KEY + sensor.getDeviceId() + ":sensorId:" + sensor.getSensorId());
                            if (object != null) {
                                if (object instanceof String) {
                                    s = object.toString();
                                }
                                if (object instanceof BigDecimal) {
                                    s = ((BigDecimal) object).toString();
                                }
                            }
                            if (s != null) {
                                map.put("pressure", s);
                            }
                        }
                        if (name.contains("低压")) {

                            String s = null;
                            Object object = redisTemplate.opsForValue().get(CacheConstants.APP_SENSOR_VALUE_KEY + sensor.getDeviceId() + ":sensorId:" + sensor.getSensorId());
                            if (object != null) {
                                if (object instanceof String) {
                                    s = object.toString();
                                }
                                if (object instanceof BigDecimal) {
                                    s = ((BigDecimal) object).toString();
                                }
                            }
                            if (s != null) {
                                map.put("lowAlarm", s);
                            }
                        }
                        if (name.contains("超压")) {
                            String s = null;
                            Object object = redisTemplate.opsForValue().get(CacheConstants.APP_SENSOR_VALUE_KEY + sensor.getDeviceId() + ":sensorId:" + sensor.getSensorId());
                            if (object != null) {
                                if (object instanceof String) {
                                    s = object.toString();
                                }
                                if (object instanceof BigDecimal) {
                                    s = ((BigDecimal) object).toString();
                                }
                            }
                            if (s != null) {
                                map.put("overAlarm", s);
                            }
                        }
                    }
                }
                mapList.add(map);
            }
            AppSensor appSensor = new AppSensor();
            appSensor.setDeviceId(appDevice.getDeviceId());
            List<AppSensor> sensorList = sensorService.selectAppSensorAllList(appSensor);
            for (AppSensor sensor : sensorList) {
                String name = sensor.getSensorName();
                if (name != null) {
                    String s = null;
                    Object object = redisTemplate.opsForValue().get(CacheConstants.APP_SENSOR_VALUE_KEY + sensor.getDeviceId() + ":sensorId:" + sensor.getSensorId());
                    if (object != null) {
                        if (object instanceof String) {
                            s = object.toString();
                        }
                        if (object instanceof BigDecimal) {
                            s = ((BigDecimal) object).toString();
                        }
                    }
                    BigDecimal bigDecimal = new BigDecimal("0");
                    if (s != null) {
                        bigDecimal = new BigDecimal(s);
                    }
                    if (name.contains("急停")) {
                        voMap.put("emergencyStop", bigDecimal.intValue() + "");
                    } else if (name.contains("温感")) {
                        voMap.put("warmDetector", bigDecimal.intValue() + "");
                    } else if (name.contains("烟感")) {
                        voMap.put("smokeDetector", bigDecimal.intValue() + "");
                    } else if (name.contains("泄露一级")) {
                        voMap.put("leakLevel1", bigDecimal.intValue() + "");
                    } else if (name.contains("泄露二级")) {
                        voMap.put("leakLevel2", bigDecimal.intValue() + "");
                    }
                }
            }
            voMap.put("bottle", mapList);
            return AjaxResult.success(voMap);
        }
        return AjaxResult.error();
    }

    @ApiOperation(value = "钢瓶电子称")
    @GetMapping("/index/gas/electronicScale/{deviceCode}")
    public AjaxResult electronicScale(@PathVariable String deviceCode) {
        AppDevice appDevice = new AppDevice();
        appDevice.setProjectId(2L);
        appDevice.setDeviceCode(deviceCode);
        Map<String, String> map = new HashMap<>();
        map.put("name", "");
        map.put("weight", "");
        map.put("alarm", "");
        List<AppDevice> deviceList = deviceService.selectAppDeviceAll(appDevice);
        if (!deviceList.isEmpty()) {
            appDevice = deviceList.get(0);
            AppSensor appSensor = new AppSensor();
            map.put("name", appDevice.getDeviceName());
            appSensor.setDeviceId(appDevice.getDeviceId());
            List<AppSensor> sensorList = sensorService.selectAppSensorAllList(appSensor);
            for (AppSensor sensor : sensorList) {
                String name = sensor.getSensorName();
                if (name != null) {
                    if (name.contains("重量") && sensor.getSensorType().equals("29") && !name.contains("报警")) {
                        String s = null;
                        Object object = redisTemplate.opsForValue().get(CacheConstants.APP_SENSOR_VALUE_KEY + sensor.getDeviceId() + ":sensorId:" + sensor.getSensorId());
                        if (object != null) {
                            if (object instanceof String) {
                                s = object.toString();
                            }
                            if (object instanceof BigDecimal) {
                                s = ((BigDecimal) object).toString();
                            }
                        }
                        if (s != null) {
                            map.put("weight", s);
                        }
                    }
                    if (name.contains("重量不足") && !name.contains("屏蔽")) {
                        String s = null;
                        Object object = redisTemplate.opsForValue().get(CacheConstants.APP_SENSOR_VALUE_KEY + sensor.getDeviceId() + ":sensorId:" + sensor.getSensorId());
                        if (object != null) {
                            if (object instanceof String) {
                                s = object.toString();
                            }
                            if (object instanceof BigDecimal) {
                                s = ((BigDecimal) object).toString();
                            }
                        }
                        if (s != null) {
                            map.put("alarm", s);
                        }
                    }
                }
            }
            return AjaxResult.success(map);
        }
        return AjaxResult.error();
    }


    @ApiOperation(value = "泄露侦测")
    @GetMapping("/index/gas/leak/{deviceCode}")
    public AjaxResult leak(@PathVariable String deviceCode) {
        AppDevice appDevice = new AppDevice();
        appDevice.setProjectId(2L);
        appDevice.setDeviceCode(deviceCode);
        Map<String, String> map = new HashMap<>();
        map.put("name", "");
        map.put("density", "");
        map.put("alarm", "");
        List<AppDevice> deviceList = deviceService.selectAppDeviceAll(appDevice);
        if (!deviceList.isEmpty()) {
            appDevice = deviceList.get(0);
            AppSensor appSensor = new AppSensor();
            map.put("name", appDevice.getDeviceName());
            appSensor.setDeviceId(appDevice.getDeviceId());
            List<AppSensor> sensorList = sensorService.selectAppSensorAllList(appSensor);
            for (AppSensor sensor : sensorList) {
                String name = sensor.getSensorName();
                if (name != null) {
                    if (name.contains("浓度") && sensor.getSensorType().equals("29") && !name.contains("报警")) {
                        String s = null;
                        Object object = redisTemplate.opsForValue().get(CacheConstants.APP_SENSOR_VALUE_KEY + sensor.getDeviceId() + ":sensorId:" + sensor.getSensorId());
                        if (object != null) {
                            if (object instanceof String) {
                                s = object.toString();
                            }
                            if (object instanceof BigDecimal) {
                                s = ((BigDecimal) object).toString();
                            }
                        }
                        if (s != null) {
                            map.put("density", s);
                        }
                    }
                    if (name.contains("浓度报警") && !name.contains("屏蔽")) {
                        String s = null;
                        Object object = redisTemplate.opsForValue().get(CacheConstants.APP_SENSOR_VALUE_KEY + sensor.getDeviceId() + ":sensorId:" + sensor.getSensorId());
                        if (object != null) {
                            if (object instanceof String) {
                                s = object.toString();
                            }
                            if (object instanceof BigDecimal) {
                                s = ((BigDecimal) object).toString();
                            }
                        }
                        if (s != null) {
                            map.put("alarm", s);
                        }
                    }
                }
            }
            return AjaxResult.success(map);
        }
        return AjaxResult.error();
    }

    @ApiOperation(value = "首页洁净房间列表")
    @GetMapping("/index/cleanRoom")
    public AjaxResult cleanRoom() {
        AppDevice appDevice = new AppDevice();
        appDevice.setType("15");
        appDevice.setProjectId(2L);
        appDevice.setParentId(0L);
        List<AppDevice> deviceList = deviceService.selectAppDeviceAll(appDevice);
        return AjaxResult.success(deviceList);
    }

    @ApiOperation(value = "首页洁净房间详情")
    @GetMapping("/index/cleanRoomAllDevice")
    public AjaxResult cleanRoomAllDevice(@RequestParam("roomDeviceCode") String roomDeviceCode, @RequestParam("airDeviceCode") String airDeviceCode, @RequestParam("exhaustDeviceCode") String exhaustDeviceCode) {
        CleanRoomVO vo = new CleanRoomVO();
        AppDevice appDevice = new AppDevice();
        appDevice.setType("15");
        appDevice.setProjectId(2L);
        appDevice.setParentId(0L);
        appDevice.setDeviceCode(roomDeviceCode);
        List<AppDevice> deviceList = deviceService.selectAppDeviceAll(appDevice);
        if (!deviceList.isEmpty()) {
            AppDevice cleanRoomDevice = deviceList.get(0);
            BeanUtils.copyBeanProp(vo, cleanRoomDevice);
            AppDevice cleanRoomEvn = new AppDevice();
            cleanRoomEvn.setParentId(cleanRoomDevice.getDeviceId());
            cleanRoomEvn.setProjectId(2L);
            cleanRoomEvn.setType("9");
            deviceList = deviceService.selectAppDeviceAll(cleanRoomEvn);
            if (!deviceList.isEmpty()) {
                AppDevice cleanRoomEvnDevice = deviceList.get(0);
                List<AppSensor> envSensorList = sensorService.selectAppSensorAllListByDeviceId(cleanRoomEvnDevice.getDeviceId());
                for (AppSensor sensor : envSensorList) {
                    String s = null;
                    Object object = redisTemplate.opsForValue().get(CacheConstants.APP_SENSOR_VALUE_KEY + sensor.getDeviceId() + ":sensorId:" + sensor.getSensorId());
                    if (object != null) {
                        if (object instanceof String) {
                            s = object.toString();
                        }
                        if (object instanceof BigDecimal) {
                            s = ((BigDecimal) object).toString();
                        }
                    }
                    switch (sensor.getSensorType()) {
                        case "1":
                            if (s != null) {
                                vo.setTemperature(s);
                            }
                            break;
                        case "2":
                            if (s != null) {
                                vo.setHumidity(s);
                            }
                            break;
                        case "3":
                            if (s != null) {
                                vo.setPressure(s);
                            }
                            break;
                    }
                }
            }
            AppDevice cleanRoomNewAir = new AppDevice();
            cleanRoomNewAir.setParentId(cleanRoomDevice.getDeviceId());
            cleanRoomNewAir.setProjectId(2L);
            cleanRoomNewAir.setType("4");
            deviceList = deviceService.selectAppDeviceAll(cleanRoomNewAir);
            if (!deviceList.isEmpty()) {
                AppDevice cleanRoomNewAirDevice = deviceList.get(0);
                List<AppSensor> newAirSensorList = sensorService.selectAppSensorAllListByDeviceId(cleanRoomNewAirDevice.getDeviceId());
                for (AppSensor sensor : newAirSensorList) {
                    String s = null;
                    Object object = redisTemplate.opsForValue().get(CacheConstants.APP_SENSOR_VALUE_KEY + sensor.getDeviceId() + ":sensorId:" + sensor.getSensorId());
                    if (object != null) {
                        if (object instanceof String) {
                            s = object.toString();
                        }
                        if (object instanceof BigDecimal) {
                            s = ((BigDecimal) object).toString();
                        }
                    }
                    if (sensor.getSensorType().equals("1")) {
                        if (s != null && !sensor.getSensorName().contains("SET")) {
                            vo.setNewAirTemperature(s);
                        }
                    }
                }
            }
        }
        return AjaxResult.success(vo);
    }


    @ApiOperation(value = "首页洁净房间排风")
    @GetMapping("/index/cleanRoom/{deviceCode}")
    public AjaxResult cleanRoomDetail(@PathVariable String deviceCode) {
        CleanRoomVO vo = new CleanRoomVO();
        AppDevice appDevice = new AppDevice();
        appDevice.setType("15");
        appDevice.setProjectId(2L);
        appDevice.setParentId(0L);
        appDevice.setDeviceCode(deviceCode);
        List<AppDevice> deviceList = deviceService.selectAppDeviceAll(appDevice);
        if (!deviceList.isEmpty()) {
            AppDevice cleanRoomDevice = deviceList.get(0);
            BeanUtils.copyBeanProp(vo, cleanRoomDevice);
            AppDevice cleanRoomEvn = new AppDevice();
            cleanRoomEvn.setParentId(cleanRoomDevice.getDeviceId());
            cleanRoomEvn.setProjectId(2L);
            cleanRoomEvn.setType("9");
            deviceList = deviceService.selectAppDeviceAll(cleanRoomEvn);
            if (!deviceList.isEmpty()) {
                AppDevice cleanRoomEvnDevice = deviceList.get(0);
                List<AppSensor> envSensorList = sensorService.selectAppSensorAllListByDeviceId(cleanRoomEvnDevice.getDeviceId());
                for (AppSensor sensor : envSensorList) {
                    String s = null;
                    Object object = redisTemplate.opsForValue().get(CacheConstants.APP_SENSOR_VALUE_KEY + sensor.getDeviceId() + ":sensorId:" + sensor.getSensorId());
                    if (object != null) {
                        if (object instanceof String) {
                            s = object.toString();
                        }
                        if (object instanceof BigDecimal) {
                            s = ((BigDecimal) object).toString();
                        }
                    }
                    switch (sensor.getSensorType()) {
                        case "1":
                            if (s != null) {
                                vo.setTemperature(s);
                            }
                            break;
                        case "2":
                            if (s != null) {
                                vo.setHumidity(s);
                            }
                            break;
                        case "3":
                            if (s != null) {
                                vo.setPressure(s);
                            }
                            break;
                    }
                }
            }
            AppDevice cleanRoomNewAir = new AppDevice();
            cleanRoomNewAir.setParentId(cleanRoomDevice.getDeviceId());
            cleanRoomNewAir.setProjectId(2L);
            cleanRoomNewAir.setType("4");
            deviceList = deviceService.selectAppDeviceAll(cleanRoomNewAir);
            if (!deviceList.isEmpty()) {
                AppDevice cleanRoomNewAirDevice = deviceList.get(0);
                List<AppSensor> newAirSensorList = sensorService.selectAppSensorAllListByDeviceId(cleanRoomNewAirDevice.getDeviceId());
                for (AppSensor sensor : newAirSensorList) {
                    String s = null;
                    Object object = redisTemplate.opsForValue().get(CacheConstants.APP_SENSOR_VALUE_KEY + sensor.getDeviceId() + ":sensorId:" + sensor.getSensorId());
                    if (object != null) {
                        if (object instanceof String) {
                            s = object.toString();
                        }
                        if (object instanceof BigDecimal) {
                            s = ((BigDecimal) object).toString();
                        }
                    }
                    BigDecimal bigDecimal = new BigDecimal(s != null ? s : "0");
                    switch (sensor.getSensorType()) {
                        case "1": //温度
                            if (s != null && !sensor.getSensorName().contains("SET")) {
                                vo.setNewAirTemperature(bigDecimal.intValue() + "");
                            }
                            break;
                        case "22": //开关机
                            vo.setNewAirPower(bigDecimal.intValue() + "");
                            break;
                        case "32": //运行模式
                            vo.setNewAirRunMode(bigDecimal.intValue() + "");
                            break;
                        case "33": //电加热1段
                            if (sensor.getSensorName().contains("DJR1")) {
                                vo.setNewAirDJR1(bigDecimal.intValue() + "");
                            } else if (sensor.getSensorName().contains("DJR2")) {
                                vo.setNewAirDJR2(bigDecimal.intValue() + "");
                            } else if (sensor.getSensorName().contains("DJR3")) {
                                vo.setNewAirDJR3(bigDecimal.intValue() + "");
                            }
                            break;
                    }
                }
            }
        }
        return AjaxResult.success(vo);
    }

    @ApiOperation(value = "首页电气所有数据")
    @GetMapping("/index/electricalCloud")
    public AjaxResult electricalCloud() {
        return weiYunApi.getData();
    }

    /**
     * 正则替换所有特殊字符
     */
    public String replaceSpecStr(String orgStr) {
        if (null != orgStr && !orgStr.trim().isEmpty()) {
            String regEx = "[`~!@#$%^&*()+=|{}':;'\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？\"]|\n|\r|\t";
            Pattern p = Pattern.compile(regEx);
            Matcher m = p.matcher(orgStr);
            return m.replaceAll("");
        }
        return null;
    }
}
